﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClassForWinForm
{
    public partial class Form13 : Form
    {
        string OldData = "";
        public Form13()
        {
            InitializeComponent();
        }

        private void Form13_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Class");
            dt.Columns.Add("Name");
            dt.Columns.Add("Sex");
            dt.Columns.Add("age");

            dt.Rows.Add("1", "大一", "张飞", "男", "20");
            dt.Rows.Add("2", "大二", "曹操", "男", "23");
            dt.Rows.Add("3", "大一", "貂蝉", "女", "18");
            dt.Rows.Add("4", "大三", "蔡文姬", "女", "20");
            dt.Rows.Add("5", "大一", "赵云", "男", "18");
            dt.Rows.Add("6", "大四", "孙权", "男", "18");
            dt.Rows.Add("7", "大三", "大乔", "女", "19");
            dt.Rows.Add("8", "大四", "刘备", "男", "22");
            dt.Rows.Add("9", "大一", "小乔", "女", "17");
            dt.Rows.Add("10", "大二", "关羽", "男", "21");

            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == -1 || e.RowIndex == -1)
            {
                return;
            }
            DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];            
            string data = cell.Value.ToString();
            if(OldData==data)
            {

            }
            else
            {

                MessageBox.Show("数据发生改动，新数据为："+data);
            }
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            OldData = (this.dataGridView1.DataSource as DataTable).Rows[e.RowIndex][e.ColumnIndex].ToString();
        }
    }
}
